<template>
  <div>
    <el-dialog
      :title="$t('select_service_order')"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="120px"
      >
        <el-form-item
          :label="$t('service_order_type_name')"
          prop="serviceOrderName"
        >
          <el-input
            v-model="queryParams.serviceOrderName"
            :placeholder="
              $t('please_enter', { name: $t('service_order_type_name') })
            "
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >{{ $t("search") }}</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
            $t("reset")
          }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableList"
        @selection-change="handleSelectionChange"
        border
        :max-height="tableHeight"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          v-if="multiple"
        />
        <el-table-column
          :label="$t('service_order_type_name')"
          align="center"
          prop="serviceOrderName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('service_order_type_code')"
          align="center"
          prop="serviceOrderCode"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('remarks')"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="[10,20,30,50,100, 200]"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUnit } from "@/api/custom/serviceUnitType";
export default {
  props: {
    //是否多选
    multiple: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      loading: false,
      open: false,
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceOrderName: null,
      },
      tableList: [],
      checkList: [],
    };
  },
  watch: {
    tableList: {
      handler(newValue) {
        if (this.checkList.length && newValue.length) {
          let list = [];
          this.tableList.forEach((item) => {
            this.checkList.forEach((val) => {
              if (val.id === item.id) {
                list.push(item);
              }
            });
          });
          if (list.length) {
            this.$nextTick(() => {
              list.forEach((row) => {
                this.$refs.multipleTable.toggleRowSelection(row, true);
              });
            });
          }
        }
      },
      immediate: true,
    },
  },
  created() {},

  methods: {
    setCheckList(list) {
      this.checkList = list;
    },
    showModal() {
      this.open = true;
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      listUnit({
        ...this.queryParams,
      }).then((res) => {
        this.tableList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.checkList = selection;
    },
    cancel() {
      this.checkList = [];
      this.open = false;
    },
    submitForm() {
      this.$emit("setCheckData", this.checkList);
      this.open = false;
    },
  },
};
</script>
<style scoped lang="scss">
.input-block {
  display: flex;
  align-items: center;
}
</style>
